package com.saoft.jcms.web.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器 请求进入controller 的时候
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    /**
     * 进入请求之前
     *
     * @param request
     * @param response
     * @param handler
     * @return true 放行继续后面的（Controller里面的）操作
     *          false 不执行后面操作（不进入controller）
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Object loginUser = request.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            //没有登录
            request.getRequestDispatcher("/405").forward(request,response);
            return false;
        }

        return super.preHandle(request, response, handler);
    }
}
